package com.shujia.flink.kafka;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo1KafkaSource {
    public static void main(String[] args) throws Exception {
        //创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //构建kafka source
        KafkaSource<String> source = KafkaSource.<String>builder()
                //指定broker列表
                .setBootstrapServers("master:9092,node1:9092,node2:9092")
                //指定topic
                .setTopics("bigdata")
                //消费者组
                .setGroupId("my-group")
                //指定读取数据的位置：earliest：读取最早的数据, latest: 读取最新的数据
                .setStartingOffsets(OffsetsInitializer.earliest())
                //读取数据的格式
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        //使用 kafka source
        DataStreamSource<String> kafkaDS = env
                .fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source");


        kafkaDS.print();


        env.execute();
    }
}
